<?php
namespace app\admin\controller;

use app\common\model\Goods as GoodsModel;
use app\common\model\Member as MemberModel;
use app\common\model\Order as OrderModel;
use app\common\model\GoodsComment as CommentModel;
/**
 * 首页控制器
 */
class Index extends Admin 
{
	public function index(){
		$today = date('Y-m-d');
		$yesterday = date('Y-m-d', strtotime('-1 day'));
		//商品总数
		$data['goods_total'] = GoodsModel::where('1=1')->count();
		//会员总数
		$data['user_total'] = MemberModel::where('1=1')->count();
		//订单总数
		$data['order_total'] = OrderModel::where('1=1')->count();
		//评价总数
		$data['comment_total'] = CommentModel::where('1=1')->count();
		
		$startTime = strtotime($today);
		$endTime = $startTime + 86400;
		//今日销售额
		$data['today_sale_money'] = OrderModel::where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->where('pay_status=1')->sum('pay_money');
		//今日下单数量
		$data['today_order_count'] = OrderModel::where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->where('pay_status=1')->count();
		//今日新增用户数量
		$data['today_user_count'] = MemberModel::where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->count();
		//今日下单用户数量
		$user_count = OrderModel::distinct(true)->where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->where('pay_status=1')->column('uid');
		$data['today_order_user'] = count($user_count);
		
		$startTime = strtotime($yesterday);
		$endTime = $startTime + 86400;
		//昨日销售额
		$data['yesterday_sale_money'] = OrderModel::where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->where('pay_status=1')->sum('pay_money');
		//昨日下单数量
		$data['yesterday_order_count'] = OrderModel::where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->where('pay_status=1')->count();
		//昨日新增用户数量
		$data['yesterday_user_count'] = MemberModel::where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->count();
		//昨日下单用户数量
		$user_count = OrderModel::distinct(true)->where('create_time', '>=', $startTime)
                ->where('create_time', '<', $endTime)->where('pay_status=1')->column('uid');
		$data['yesterday_order_user'] = count($user_count);

		//一周交易图
		$weekDate = $this->getWeekDate();
		$data['echarts_date'] = json_encode($weekDate);		
		$data['echarts_order_total'] = json_encode($this->getOrderTotal($weekDate));
		$data['echarts_order_count'] = json_encode($this->getOrderCount($weekDate));

		$this->assign('data', $data);
		return $this->fetch();
	}

	/**
	 * 获取最近一周的日期
	 */
	private function getWeekDate(){
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
	}

	/**
     * 获取订单总量 (指定日期)
     * @param $days
     * @return array
     */
    private function getOrderTotal($days)
    {    	
        $data = [];
        foreach ($days as $day) {
        	$startTime = strtotime($day);
			$endTime = $startTime + 86400;
            $data[] = OrderModel::where('create_time', 'between time', ['{$startTime}', '{$endTime}'])->where('pay_status=1')->sum('pay_money');
        }
        return $data;
    }

    /**
     * 获取订单总量 (指定日期)
     * @param $days
     * @return array
     */
    private function getOrderCount($days)
    {    	
        $data = [];
        foreach ($days as $day) {
        	$startTime = strtotime($day);
			$endTime = $startTime + 86400;
            $data[] = OrderModel::where('create_time', 'between time', ['{$startTime}', '{$endTime}'])->where('pay_status=1')->count();
        }
        return $data;
    }
}